﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDD;
using Xunit;

namespace DokiDokiTest {
    public class TestTransformMesh {
        [Fact]
        public void Test_New1 () {
            var msh = new Transformer ();

            Assert.Equal (1, msh.SubmeshCount);
            Assert.Equal (null, msh.FeedbackBuffer);
            Assert.Equal (null, msh.VertexBuffer);
            Assert.Equal (null, msh.Appearance);
            Assert.Equal (0, msh.FeedbackBuffers.Count ());
            Assert.Equal (0, msh.VertexBuffers.Count ());
            Assert.Equal (0, msh.Appearances.Count ());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <remarks>
        /// (*1) 列挙子はnull要素を含めないのが仕様。
        /// </remarks>
        [Fact]
        public void Test_New2 () {
            var msh = new Transformer (2);

            Assert.Equal (2, msh.SubmeshCount);                   // (*1)
            Assert.Equal (null, msh.FeedbackBuffer);
            Assert.Equal (null, msh.VertexBuffer);
            Assert.Equal (null, msh.Appearance);
            Assert.Equal (0, msh.FeedbackBuffers.Count ());        // (*1)
            Assert.Equal (0, msh.VertexBuffers.Count ()); // (*1)
            Assert.Equal (0, msh.Appearances.Count ());   // (*1)
        }

        [Fact]
        public void Test_Set () {
            var msh = new Transformer (2);
            var fbuf = new FeedbackBuffer ();
            var vbuf = new VertexBuffer ();
            var app = new Appearance ();

            msh.SetFeedbackBuffer (fbuf);
            msh.SetVertexBuffer (vbuf);
            msh.SetAppearance (app);

            Assert.Equal (fbuf, msh.FeedbackBuffer);
            Assert.Equal (vbuf, msh.VertexBuffer);
            Assert.Equal (app, msh.Appearance);
        }


        [Fact]
        public void Test_SetFeedbackBuffer () {
            var msh = new Transformer (2);
            var fbuf1 = new FeedbackBuffer ();
            var fbuf2 = new FeedbackBuffer ();

            msh.SetFeedbackBuffer (0, fbuf1);
            msh.SetFeedbackBuffer (1, fbuf2);

            Assert.Equal (2, msh.FeedbackBuffers.Count ());
            Assert.Equal (fbuf1, msh.GetFeedbackBuffer (0));
            Assert.Equal (fbuf2, msh.GetFeedbackBuffer (1));
        }

        [Fact]
        public void Test_SetTransformVertexBuffer () {
            var msh = new Transformer (2);
            var vbuf1 = new VertexBuffer ();
            var vbuf2 = new VertexBuffer ();

            msh.SetVertexBuffer (0, vbuf1);
            msh.SetVertexBuffer (1, vbuf2);
           
            Assert.Equal (2, msh.VertexBuffers.Count());
            Assert.Equal (vbuf1, msh.GetVertexBuffer (0));
            Assert.Equal (vbuf2, msh.GetVertexBuffer (1));
        }

        [Fact]
        public void Test_SetTransformAppearance () {
            var msh = new Transformer (2);
            var app1 = new Appearance ();
            var app2 = new Appearance ();

            msh.SetAppearance (0, app1);
            msh.SetAppearance (1, app2);

            Assert.Equal (2, msh.Appearances.Count ());
            Assert.Equal (app1, msh.GetAppearance (0));
            Assert.Equal (app2, msh.GetAppearance (1));
        }

        [Fact]
        public void Test_References () {
            var msh = new Transformer (2);
            var fbuf = new FeedbackBuffer ();
            var vbuf1 = new VertexBuffer ();
            var vbuf2 = new VertexBuffer ();
            var app1 = new Appearance ();
            var app2 = new Appearance ();

            msh.SetFeedbackBuffer (fbuf);
            msh.SetVertexBuffer (0, vbuf1);
            msh.SetVertexBuffer (1, vbuf2);
            msh.SetAppearance (0, app1);
            msh.SetAppearance (1, app2);

            var objs = msh.References;

            Assert.Equal (5, objs.Count ());
            Assert.Equal (true, objs.All (x => x != null));
        }


        [Fact]
        public void Test_Find () {
            var msh = new Transformer (2);
            var fbuf = new FeedbackBuffer ();
            var vbuf1 = new VertexBuffer ();
            var vbuf2 = new VertexBuffer ();
            var app1 = new Appearance ();
            var app2 = new Appearance ();

            msh.SetFeedbackBuffer (fbuf);
            msh.SetVertexBuffer (0, vbuf1);
            msh.SetVertexBuffer (1, vbuf2);
            msh.SetAppearance (0, app1);
            msh.SetAppearance (1, app2);

            fbuf.UserID = 101;
            vbuf1.UserID = 102;
            vbuf2.UserID = 103;
            app1.UserID = 104;
            app2.UserID = 105;
            msh.UserID = 106;

            Assert.Equal (fbuf, msh.Find (101));
            Assert.Equal (vbuf1, msh.Find (102));
            Assert.Equal (vbuf2, msh.Find (103));
            Assert.Equal (app1, msh.Find (104));
            Assert.Equal (app2, msh.Find (105));
            Assert.Equal (msh, msh.Find (106));
        }
    }
}
